package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.vo.SpecificationVo;
import com.dongdongshop.vo.TemplateVo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.util.resources.ga.LocaleNames_ga;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/goodsController")
public class GoodsController {
    @DubboReference
    private GoodsService goodsService;

    @DubboReference
    private TbItemCatService tbItemCatService;

    @DubboReference
    private TypeTemplateService typeTemplateService;

    @DubboReference
    private SpecificationService specificationService;


    private Logger logger = LoggerFactory.getLogger(GoodsController.class);


    @RequestMapping("/toGoodsEdit")
    public String toGoodsEdit () {
        return "/admin/goods_edit";
    }
    @RequestMapping("/selectGoodsHome")
    @ResponseBody
    public Result selectGoodsHome(@RequestParam(defaultValue = "0") Long parentId){
        try {
            List<TbItemCat> list = tbItemCatService.listItemCat(parentId);
            return Result.OK().setData(list);
        }catch (Exception e){
            logger.error("分类列表查询失败 {}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/selectTemplate")
    @ResponseBody
    public Result selectTemplate(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.selectTemplateById(id);
            return Result.OK().setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error("模板列表查询失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/selectYes")
    @ResponseBody
    public Result selectYes(Long id){
        try {
            TbItemCat tbItemCat  = tbItemCatService.selectItemById(id);
            return Result.OK().setData(tbItemCat);
        }catch (Exception e){
            logger.error("模板列表查询失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/saveGoods")
    @ResponseBody
    public Result saveGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,String itemS){

        TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();//调用LoginRealm,存的登录对象.
        tbGoods.setSellerId(seller.getSellerId());

        List<TbItem> tbItems = JSONUtil.toList(itemS,TbItem.class);
        for (TbItem tbItem : tbItems) {
            tbItem.setSellerId(seller.getSellerId());
            tbItem.setSeller(seller.getName());

            //通过LocalDateTime.now()获取当前的时间，具体到年月日时分秒
            LocalDateTime localDateTime = LocalDateTime.now();
            //Zoneld用于标识在Instant和LocalDateTime之间的转换规则，同时也是时区ID
            //通过ZoneId的systemDefault获取系统默认时区
            ZoneId zoneId = ZoneId.systemDefault();
            //LocalDateTime只是能显示本地日期和时间，没有时区
            //ZoneIdDateTime要标识一个带时区的日期和时间
            //ZonedDateTime相当于LocalDateTime+ZoneId
            //通过LocalDateTime的atZone(new ZoneID)把LocalDateTime转换为ZoneDateTime
            ZonedDateTime zonedDateTime =localDateTime.atZone(zoneId);
            //Instant类用于在应用程序中记录事件的时间戳
            //也即是把时间转化为一瞬间
            //ZonedDateTime.toInstant是用于把ZonedDateTime转为Instant类型
            Instant instant = zonedDateTime.toInstant();
            //使用Date.from(new instant)方法,改为Date类型,该日期表示时间轴上与传递的瞬间相同的点。
            Date date = Date.from(instant);

            tbItem.setCreateTime(date);
            tbItem.setUpdateTime(date);
        }
        try {
            goodsService.saveGoods(tbGoods,tbGoodsDesc,tbItems);
            return Result.OK();
        }catch (Exception e){
            logger.error("模板列表查询失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/selectTemplateSpec")
    @ResponseBody
    public Result selectTemplateSpec(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.selectTemplateById(id);
            //获取规格的字符串
            String specIds = tbTypeTemplate.getSpecIds();
            //将字符串转成对象
            List<SpecificationVo> specificationVos = JSONUtil.toList(specIds, SpecificationVo.class);
            for (SpecificationVo specVo : specificationVos) {
                SpecificationVo specificationVo = specificationService.selectSpecificationAndOptionById(specVo.getId());
                //将查询出来的规格选项数据放入SpecificationVo类中
                specVo.setOptions(specificationVo.getOptions());
            }
            TemplateVo templateVo = new TemplateVo();
            BeanUtils.copyProperties(tbTypeTemplate,templateVo);
            templateVo.setSpecVoList(specificationVos);
            return Result.OK().setData(templateVo);
        }catch (Exception e){
            logger.error("模板列表查询失败:{}",e);
            return Result.ER();
        }

    }
}
